﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using KZ.Exception;
using KZ.Lottery.BLL;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery
{
    public partial class FrmResult : Form
    {
        #region Private Methods

        private void FrmResult_Load(object sender, EventArgs e)
        {
            dtpFrom.Value = DateTime.Now.AddDays(-1);

            LoadChannel();
            LoadResult();
        }

        private void LoadChannel()
        {
            try
            {
                cmbChannelID.DataSource = ChannelBLL.GetChannelListForCMB();
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadChannel", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void dtgResult_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                FrmResultInsert frm = new FrmResultInsert();
                frm.ResultID = Utility.StringParse(dtgResult[colResultID.Index, dtgResult.CurrentCell.RowIndex].Value);
                frm.Action = KZ_Resource.Action_Update;
                frm.ShowDialog();
            }
        }

        private void dtgResult_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && dtgResult.SelectedRows.Count > 0)
            {
                tsmiDelete_Click(tsmiDelete, null);
            }
        }

        private void contextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            tsmiUpdate.Enabled = dtgResult.RowCount > 0;
            tsmiDelete.Enabled = dtgResult.RowCount > 0;
        }

        private void tsmiInsert_Click(object sender, EventArgs e)
        {
            FrmResultInsert frm = new FrmResultInsert();
            frm.Action = KZ_Resource.Action_Insert;
            frm.ShowDialog();
        }

        private void tsmiUpdate_Click(object sender, EventArgs e)
        {
            FrmResultInsert frm = new FrmResultInsert();
            frm.ResultID = Utility.StringParse(dtgResult[colResultID.Index, dtgResult.CurrentCell.RowIndex].Value);
            frm.Action = KZ_Resource.Action_Update;
            frm.ShowDialog();
        }

        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (DialogResult.Yes == MessageBox.Show(KZ_Message.BLM_CFM_DELETE, String.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    if (ResultBLL.DeleteResult(ResultID))
                    {
                        LoadResult();
                        Global.SetMessage(lblMessage, KZ_Message.BLM_SUC_RESULT_DELETE, true);
                    }
                }
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (BusinessLogicException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("btnApprove_Click", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadResult();
        }

        private void btnApprove_Click(object sender, EventArgs e)
        {
            try
            {
                if (DialogResult.Yes == MessageBox.Show(KZ_Message.BLM_CFM_APPROVE, String.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    Cursor = Cursors.WaitCursor;

                    if (ResultBLL.ApproveResult(ResultList))
                    {
                        LoadResult();
                        Global.SetMessage(lblMessage, KZ_Message.BLM_SUC_RESULT_APPROVE, true);
                    }
                }
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (BusinessLogicException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("btnApprove_Click", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        #endregion Private Methods

        #region Constructors

        public FrmResult()
        {
            InitializeComponent();
            dtgResult.AutoGenerateColumns = false;

            dtpFrom.CustomFormat = Global.Culture.DateTimeFormat.ShortDatePattern;
            dtpTo.CustomFormat = Global.Culture.DateTimeFormat.ShortDatePattern;
            colResultDate.DefaultCellStyle.Format = Global.Culture.DateTimeFormat.ShortDatePattern;

            cmbChannelID.DisplayMember = ChannelColumns.ChannelName.ToString();
            cmbChannelID.ValueMember = ChannelColumns.ChannelID.ToString();
        }

        #endregion Constructors

        #region Public Members

        public string ChannelID
        {
            get { return Utility.StringParse(cmbChannelID.SelectedValue); }
        }

        public string ResultID
        {
            get
            {
                string resultID = String.Empty;
                foreach (DataGridViewRow row in dtgResult.SelectedRows)
                {
                    if (Utility.StringParse(row.Cells[colStatus.Index].Value) == KZ_Resource.ResultStatus_Approved)
                    {
                        throw new BusinessLogicException(KZ_Message.BLM_ERR_RESULT_APPROVED_NOTDELETE);
                    }
                    resultID += "'" + Utility.StringParse(row.Cells[colResultID.Index].Value) + "',";
                }
                if (resultID.EndsWith(",")) { resultID = resultID.Remove(resultID.Length - 1); }
                return resultID;
            }
        }

        public List<Result> ResultList
        {
            get
            {
                List<Result> resultList = new List<Result>();
                foreach (DataGridViewRow row in dtgResult.SelectedRows)
                {
                    if (Utility.StringParse(row.Cells[colStatus.Index].Value) == KZ_Resource.ResultStatus_Approved)
                    {
                        throw new BusinessLogicException(KZ_Message.BLM_ERR_RESULT_APPROVED_NOTAPPROVE);
                    }

                    resultList.Add(new Result
                    {
                        ResultID = Utility.StringParse(row.Cells[colResultID.Index].Value),
                        ResultDate = Utility.DateTimeParse(row.Cells[colResultDate.Index].Value),
                        ChannelID = Utility.StringParse(row.Cells[colChannelID.Index].Value),
                        Head = Utility.StringParse(row.Cells[colHead.Index].Value),
                        Tail = Utility.StringParse(row.Cells[colTail.Index].Value)
                    });
                }
                return resultList.OrderBy(r => r.ResultDate).ToList();
            }
        }

        #endregion Public Members

        #region Public Memthods

        public void LoadResult()
        {
            try
            {
                dtgResult.DataSource = ResultBLL.GetResultList(ChannelID, dtpFrom.Value, dtpTo.Value);
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadResult", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        #endregion Public Memthods
    }
}